package com.taobao.top.demo.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.taobao.top.demo.dataobject.Item;
import com.taobao.top.demo.exception.DemoException;
import com.taobao.top.demo.getData.GetItem;
/**
 * 获得iid
 * @author Jessica
 *
 */
public class GetIidServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			//从session中获取sessionKey和nick
			String sessionKey = (String)req.getSession().getAttribute("sessionKey");
			String nick = (String)req.getSession().getAttribute("nick");
			
			//从参数中获取iid
			String iid = req.getParameter("iid");
			//如果为空，则是从UpdateImgServlet跳转过来，获得iid
			if(iid == null){
				iid = (String)req.getAttribute("iid");
			}
			//得到商品
			Item item = GetItem.getItem(sessionKey, nick, iid);
			req.setAttribute("iid", iid);
			req.setAttribute("item", item);
			//转到页面，显示内容
			req.getRequestDispatcher("/operation/showItem.jsp").forward(req, resp);
		} catch (DemoException e) {
			String errorMsg = "获得商品失败！";
			req.setAttribute("errorMsg", errorMsg);
			req.getRequestDispatcher("/error.jsp").forward(req, resp);
		}
	}

}
